package com.gimi.cloud.mmf.user.service.impl;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import com.gimi.cloud.mmf.user.dao.TestUserDao;
import com.gimi.cloud.mmf.user.entity.TestUser;
import com.gimi.cloud.mmf.user.service.TestUserService;

@Service
public class TestUserServiceImpl implements TestUserService{
	
	@Autowired private TestUserDao testUserDao;

	@Override
	@Transactional
	public void save(TestUser user) {
		testUserDao.save(user); //保存，值引用不用获取返回值
		user.setAge(22);
		testUserDao.save(user); //更新
	}

	@Override
	public List<TestUser> queryByHql(String userName, Pageable pageable) {
		return testUserDao.queryByHql(userName, pageable);
	}

	@Override
	public List<TestUser> queryBySql(String userName, Pageable pageable) {
		return testUserDao.queryBySql(userName);
	}

	@Override
	public List<TestUser> queryByMethodName(String userName, Pageable pageable) {
		return testUserDao.findByUserName(userName, pageable);
	}

	@Override
	public List<Map<String, Object>> queryMoreTableByHql(String userName, Pageable pageable) {
		return testUserDao.queryMoreTableByHql(userName, pageable);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Page<List<Map<String, Object>>> queryMoreTableBySql(String userName, Pageable pageable) {
		String sql = "select tu.* , tcl.money, tcl.purpose from test_user tu join test_capital_log tcl on tu.id = tcl.user_id where user_name = ?";
		return  (Page<List<Map<String, Object>>>) testUserDao.findPageByParams(sql, pageable, userName);
	}

}
